<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>骡窝日报详情</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/newsContent.css">
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <link rel="stylesheet" href="/css/travelContent.css"/>
    <!-- 引入jquery 和 common.js 可以获取到地址栏上拼接的参数 -->
    <script type="text/javascript" src="/js/common.js"></script>


    <script>
        $(function () {
            // 先从地址栏上获取当前游记的i的,发送请求获取对应的id的日报信息,渲染到页面中
            var params = getParams(); // 获取地址栏上的数据信息转成key=value的形式
            if (params.id) {
                // 发送Ajax请求查询对应id的日报内容
                $.get("/dailies/" + params.id, function (data) {
                    console.log(data);
                    // 数据渲染
                    $(".newsDetail").renderValues(data);
                    console.log(data.content);
                });
            }

            //收藏
            collect();//加载页面时默认调用一次渲染收藏数目的方法
            //查询是否被收藏
            function collect() {
                $.get("/collects/3/" + params.id, function (data) {
                    console.log(data);
                    if (data) {
                        $("#collectBtn").css("color", "red");
                    } else {
                        $("#collectBtn").css("color", "black");

                    }
                });

            };
            //计算收藏的数量
            function countCollect() {
                $.get("/collects/count/3/" + params.id, function (data) {
                    $(".collectBtn").renderValues({collectNum:data});

                })
            }
            countCollect();

            var user = JSON.parse(sessionStorage.getItem("user"));
            $("#collectBtn").click(function () {
                if (user) {
                    $.get("/collects/3" + "/" + params.id, function (data) {
                        if (data) {
                            // 说明用户已经收藏,提示用户,并且取消收藏
                            $.ajax({
                                type: "DELETE",
                                url: "/collects/3" + "/" + params.id,
                                success: function () {
                                    $(document).dialog({
                                        type: 'notice',
                                        infoText: '亲,收藏已经取消成功!',
                                        autoClose: 1000,
                                        position: 'bottom'  // center: 居中; bottom: 底部
                                    });
                                    collect();
                                    countCollect();
                                }
                            })
                        } else {
                            $.post("/collects/3" + "/" + params.id, function (data) {
                                if (data.success) {
                                    $("#collectBtn").css("color", "red");
                                }
                                countCollect();
                            });
                        }
                    });
                } else {
                    // alert("亲,收藏请先登录");
                    $(document).dialog({
                        type: 'confirm',
                        closeBtnShow: true,
                        content: '请登陆后再执行相关操作!',
                        onClickConfirmBtn: function () {
                            window.location.href = "/login.html"
                        }
                    });
                }
            });
        });
    </script>
</head>

<body>

<div class="newsDetail">
    <div>
        <a href="javascript:history.go(-1);" style="position: absolute;top: 10px;left: 22px;">
            <span><i class="fa fa-chevron-left fa-2x" style="color: white;"></i></span>
        </a>
        <img render-src="coverUrl">

        <div class="container">
            <div class="row typeRow">
                <div class="col-4">
                    <span class="type">美图美文</span>
                </div>
                <div class="col-2">
                </div>
                <div class="col-2">
                    <span>By</span>
                </div>
                <div class="col-4">
                    <span class="type">骡窝官方</span>
                </div>
            </div>

            <div class="detail">
                <h2 class="title" render-html="title"></h2>
                <div class="content" render-html="dailyContent.content">
                </div>
            </div>

            <hr>
            <div class="container d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-thumbs-o-up" id="likeBtn"> </i>
                    <span>0</span><!--点赞-->
                </div>
                <div class="p-2  flex-fill"><i class="fa  fa-commenting-o" id="commentBtn"> </i>
                    <span id="commentCount"></span><!--评论-->
                </div>
                <div class="p-2  flex-fill collectBtn">
                    <i class="fa fa-heart-o handleBtn" id="collectBtn" render-html="collectNum"></i>
                </div>
            </div>
        </div>
    </div>
</div>


</body>

</html>